﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;
using PloobsEngine.SceneControl;
using PloobsEngine.Utils;
using Etapa1.SofRender;
using System.Collections.Generic;
using ModelXML;
using PloobsEngine.Cameras;
using Etapa1.Import;
using Etapa1.SofRender.Sample;

namespace Etapa1.Screens
{
    public class SoftAlphaModelScreen : IScreen
    {
        public SoftAlphaModelScreen()
            : base()
        {
        }

        Device device ;
        CameraStatic camera;
        List<SModelo> modelos = new List<SModelo>();

        public override void LoadContent(EngineStuff es)
        {
            device = new Device();            
            Cena cena = Cena.LoadXmlContent(@"C:\Users\tpastor\Documents\3dsMax\export\cam.xml");

            foreach (var item in cena.Modelos)
            {                
                SModelo m = new SModelo(item);
                TextureShader ts = new TextureShader();
                ts.Difuse = m.Difuse;                
                
                m.Effect = ts;
                modelos.Add(m);
            }

            List<CameraStatic> cameras = new List<CameraStatic>();
            foreach (var item in cena.Cameras)
            {
                CameraStatic cs = new CameraStatic(ImporterHelper.toVector3(item.Position), ImporterHelper.toVector3(item.Target));
                //cs.FieldOfView = item.Fov;
                cs.NearPlane = item.NearPlane;
                cs.FarPlane = item.FarPlane;
                cs.Name = item.Name;
                cameras.Add(cs);
            }
            camera = cameras[0];

            base.LoadContent(es);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            device.Clear(Color.Red, 1);
            device.AlphaEnabled = true;
            device.AlphaFunction = AlphaBlendingFunctions.ClassicalAlpha;

            foreach (var item in modelos)
            {
                TextureShader bs = (item.Effect as TextureShader);
                bs.World = item.World;
                bs.View = camera.View;
                bs.Projection = camera.Projection;
                device.SetEffect(item.Effect);
                device.SetIndexBuffer(item.Indices);
                device.SetVertexBuffer(item.VertexType);
                device.DrawIndexedPrimitive(PrimitivesType.TriangleList);                
            }           
            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            device.Draw();
            base.Draw(gameTime);

        }
    }
        
}

